﻿using Common;
using CommonModel.Constant;
using IBll;
using System;
using System.IO;
using System.Web;

namespace Bll
{
    /// <summary>
    /// 文件服务
    /// </summary>
    public class FileBll : IFileBll
    {
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="customFolderName"></param>
        /// <returns></returns>
        public bool UploadFile(string customFolderName)
        {
            bool result = false;

            HttpFileCollection httpFileCollection = HttpContext.Current.Request.Files;
            if (httpFileCollection.Count > 0)
            {
                LogHelper.Debug("需要上传的文件数：" + httpFileCollection.Count);
                for (int i = 0; i < httpFileCollection.Count; i++)
                {
                    //文件相对路径
                    string fileRelativePath = GetFileRelativePath(customFolderName, httpFileCollection[i]);
                    //文件绝对路径
                    string fileAbsolutePath = $"{AppDomain.CurrentDomain.BaseDirectory}{fileRelativePath}";
                    //文件绝对路径目录
                    string fileAbsolutePathDirectory = Path.GetDirectoryName(fileAbsolutePath);
                    if (!Directory.Exists(fileAbsolutePathDirectory))
                    {
                        Directory.CreateDirectory(fileAbsolutePathDirectory);
                    }
                    httpFileCollection[i].SaveAs(fileAbsolutePath);
                    LogHelper.Debug("上传文件成功：" + fileRelativePath);
                    #region fileRelativePath根据customFolderName，入不同表字段保存

                    #endregion
                }
                result = true;
            }
            else
            {
                LogHelper.Debug("没有文件可上传");
            }
            LogHelper.Debug("上传文件结束");

            return result;
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public void DownloadFile(string filePath)
        {
            string fileAbsolutePath = HttpContext.Current.Server.MapPath(filePath);
            LogHelper.Debug("下载文件：" + filePath);
            if (File.Exists(fileAbsolutePath))
            {
                FileDownHelper.DownLoad(filePath);
                LogHelper.Debug("下载文件结束：" + filePath);
            }
        }

        #region 私有方法
        /// <summary>
        /// 获取文件自定义相对路径
        /// </summary>
        /// <param name="customFolderName"></param>
        /// <param name="httpPostFile"></param>
        /// <returns></returns>
        private string GetFileRelativePath(string customFolderName, HttpPostedFile httpPostFile)
        {
            string fileExtension = Path.GetExtension(httpPostFile.FileName);
            //文件名
            string fileName = StringHelper.GetGuidString() + fileExtension;
            return $"{RelativePath.UploadFileFolder}/{customFolderName}{StringHelper.GetYearMonthDayFolderPath()}/{fileName}";
        }
        #endregion
    }
}
